﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace 自定图标类
{
    public class chartClass
    {
        Chart chart = new Chart();
        Legend legend = new Legend();
        Series series = new Series();
        ChartArea chartarea = new ChartArea();
        public List<int> points = new List<int>();

        public chartClass(string chartStr, string type)
        {

            //设定chart的长宽
            Size size = new Size();
            size.Height = 100;
            size.Width = 800;
            chart.Size = size;

            //设定显示的值
            DataPoint dp = new DataPoint();
            dp.IsEmpty = true;

            //设定series
            series.Name = chartStr;//图标序列的名称
            series.ChartType = SeriesChartType.StepLine;//设定序列样式
            //series.Points.DataBindY(value);//将值和数据绑定
            series.Points.Add(dp);
            

            //设定图表区样式
            chartarea.Name= "chartarea";
            chartarea.AxisX.MajorGrid.Enabled = false;//取消X轴的线
            chartarea.AxisY.MajorGrid.Enabled = false;//取消Y轴的线
            chartarea.AxisX.Enabled = AxisEnabled.False;//禁用X坐标轴
            chartarea.AxisY.Enabled = AxisEnabled.False;//禁用Y坐标轴
            chartarea.BackColor = Color.AliceBlue;//设定区域的背景颜色
            //chartarea.AxisY.Maximum = 50;//设定Y轴的最大值
            //chartarea.AxisY.Minimum = -50;//设定Y轴的最小值



            //设定图例样式
            legend.DockedToChartArea = chartarea.Name;

            switch (type)
            {
                case "bool":
                    //chartarea.AxisY.Interval = 1;
                    chartarea.AxisX.Interval = 1;
                    //chartarea.AxisY.Maximum = 1;
                    //chartarea.AxisY.Minimum = 0;
                    chartarea.AxisX.Maximum = 500;
                    chartarea.AxisX.Minimum = 0;
                    size.Height = 40;
                    break;
                default:
                    chartarea.AxisX.Interval = 1;
                    chartarea.AxisX.Maximum = 500;
                    chartarea.AxisX.Minimum = 0;
                    size.Height = 200;
                    break;
            }

            chart.ChartAreas.Add(chartarea);//在chart上面添加一个chartarea;
            chart.Series.Add(series);//添加曲线
            chart.Legends.Add(legend);//添加图例
            chart.Size = size;
            
            ////启用双缓
            ////首先定义listView的Type类
            //Type dgvType = this.chart.GetType();

            ////通过Type类，反射出DoubleBuffered的属性值。
            ////System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic:表示寻找的范围是示例成员以及非公开成员
            //_PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

            ////将DoubleBuffered属性值设置为True
            //pi.SetValue(this.chart, true, null);
        }
        public Chart 返回图表()
        {
            return chart;
        }
        public void SHOW()
        {
            chart.Series[0].Points.DataBindY(points);
        }
    }
}
